# import logging
# import sys
#
#
# def _get_logger():
#     log = logging.getLogger('log')
#     log.setLevel(logging.INFO)
#     formatter = logging.Formatter('[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s',
#                                   datefmt='%Y-%m-%d %H:%M:%S')
#     # 配置输出日志到终端的 Handler
#     stream_handler = logging.StreamHandler()
#     stream_handler.setFormatter(formatter)
#     # 配置保存日志到文件的 Handler
#     file_handler = logging.FileHandler("internal_server.log", encoding='utf-8')
#     file_handler.setFormatter(formatter)
#     # 添加 Handler
#     log.addHandler(file_handler)
#     log.addHandler(stream_handler)
#     return log
#
#
# # 日志句柄
# logger = _get_logger()


import os
import sys
import uuid

from loguru import logger as loguru_logger

import settings

my_log_file_path = "internal_server.log"


class MyLogger:
    def __init__(self, log_file_path=my_log_file_path):
        self.logger = loguru_logger
        # 清空所有设置
        self.logger.remove()
        # 添加控制台输出的格式,sys.stdout为输出到屏幕;关于这些配置还需要自定义请移步官网查看相关参数说明
        if not settings.ONLINE:
            self.logger.add(sys.stdout, enqueue=True,
                            format="[{level}]<green>[{time:YYYY-MM-DD HH:mm:ss}]</green> | "  # 颜色>时间
                                   "{process.name} | "  # 进程名
                                   "{thread.name} | "  # 进程名
                                   "<cyan>{module}</cyan>.<cyan>{function}</cyan>"  # 模块名.方法名
                                   ":<cyan>{line}</cyan> | "  # 行号
                                   "<level>{level}</level>: "  # 等级
                                   "<level>{message}</level>"  # 日志内容
                            )
        # 输出到文件的格式,注释下面的add',则关闭日志写入
        self.logger.add(log_file_path, level='INFO', enqueue=True,
                        format='[{level}][{time:YYYY-MM-DD HH:mm:ss}][{file.name}:{line}] - '  # 时间
                               "{process.name} | "  # 进程名
                               "{thread.name} | "  # 进程名
                               ' -{message}',  # 模块名.方法名:行号
                        rotation="1 day", retention="3 days")

    def get_logger(self):
        return self.logger


logger = MyLogger().get_logger()
